package com.gtpickupapp;

import java.util.ArrayList;

import android.app.ActivityGroup;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class NewEventGroup extends ActivityGroup {

	public AlertDialog.Builder exitAlert;
	public static NewEventGroup group;
	private ArrayList<View> history;
	
	@Override  
    protected void onCreate(Bundle savedInstanceState) {  
		super.onCreate(savedInstanceState);  
		this.history = new ArrayList<View>();
		setupAlerts();
		group = this;  
		Bundle bundle = getIntent().getExtras();
		Intent intent = new Intent(this, NewEvent.class);
		intent.putExtras(bundle);
		// Start the root activity within the group and get its view  
		View view = getLocalActivityManager().startActivity("new", intent  
                .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))  
                .getDecorView();  
		// Replace the view of this ActivityGroup  
		replaceView(view);  
    }
	
	public void replaceView(View v) {  
        // Adds the old one to history  
		 history.add(v);  
        // Changes this Groups View to the new View.  
		 setContentView(v);  
	 }
	 
	 public void back() {  
        if(history.size() > 1) {  
            history.remove(history.size()-1);  
            setContentView(history.get(history.size()-1));  
          
        }else {  
        	exitAlert.create().show();
        	//finish();  
        }  
	  } 
	 
	 @Override  
	 public void onBackPressed() {  
		 NewEventGroup.group.back();  
	     return;  
	 } 
	 
	 public void setupAlerts(){
		 exitAlert = new AlertDialog.Builder(this);
	        exitAlert.setMessage("Are You Sure You Want To Exit The Application")
	        	.setCancelable(false).setPositiveButton("OK", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						finish();
					}
				})
				.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						dialog.cancel();
					}
				});
	        
	 }
	
}
